/*
 *  (c) 2005 Zhijie Chen <zigzag.chen@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Swato.Form = Class.create();
Swato.Form.prototype = {

	initialize : function(form) {
		this.form = form;
	},

	toJSON : function() {
		var formObject=new Object();
		var elements = Form.getElements($(this.form));
		for (var i=0;i < elements.length;i++){
			if(elements[i].exclude){
				continue;
			}
			var isSerializable = Form.Element.serialize(elements[i]);
			if (isSerializable){
				formObject[elements[i].name]=$F(elements[i]);
			}	
		}
		return formObject;
	},

	onSuccess : function(result) {
		var elements = Form.getElements($(this.form));
		for (var i=0;i < elements.length;i++){
			if (result[elements[i].name]!=null) {
				FormUtil.setValue(elements[i],result[elements[i].name]);
			}
		}
	}
};

var FormUtil = { // Based on code from http://getahead.ltd.uk/dwr
	isArray : function(data) {
		return (data && data.join) ? true : false;
	},
	isHTMLElement : function(ele, nodeName) {
		if (nodeName == null) {
			return ele != null &&
				typeof ele == "object" &&
				ele.nodeName != null;
		} else {
			return ele != null &&
				typeof ele == "object" &&
				ele.nodeName != null &&
				ele.nodeName.toLowerCase() == nodeName.toLowerCase();
		}
	},
	isHTMLInputElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "input");
	},
	isHTMLTextAreaElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "textarea");
	},
	isHTMLSelectElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "select");
	},
	setValue : function(ele,val) {
		if (FormUtil.isHTMLSelectElement(ele))
		{
			var found  = false;
			var i;
			for (i = 0; i < ele.options.length; i++) {
				if (ele.options[i].value == val){
					ele.options[i].selected = true;
					found = true;
				} else {
					ele.options[i].selected = false;
				}
			}
			if (found) {
				return;
			}
			for (i = 0; i < ele.options.length; i++) {
				if (ele.options[i].text == val) {
					ele.options[i].selected = true;
					break;
				}
			}
			return;
		}
		if (FormUtil.isHTMLInputElement(ele)) {
			switch (ele.type) {
			case "checkbox":
			case "check-box":
			case "radio":
				ele.checked = (val == true);
				return;
			case "hidden":
			case "text":
				ele.value = val;
				return;
			default:
				alert("Not sure how to setValue on a input element of type " + ele.type);
				ele.value = val;
				return;
			}
		}
		if (FormUtil.isHTMLTextAreaElement(ele)) {
			ele.value = val;
			return;
		}
	}
};